package cn.rxmind.dlicense.core.rule;

import com.google.common.collect.Sets;

import java.util.Set;
import java.util.function.Predicate;

/**
 * 包含/排除规则，排除优先
 * 继承了 Predicate 之后获得了规则组合的能力
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/16 2:28 下午
 */
public interface IncludeAndExcludeRule<T> extends Predicate<Set<T>> {

    /**
     * 获取包含配置
     * @return
     */
    Set<T> getInclude();

    /**
     * 获取排除配置
     * @return
     */
    Set<T> getExclude();

    /**
     * 排除 && 包含
     * @param values
     * @return
     */
    @Override
    default boolean test(Set<T> values){

        if(getExclude() != null && !getExclude().isEmpty() && !Sets.intersection(values,getExclude()).isEmpty()){
            return false;
        }
        return getInclude() == null || getInclude().isEmpty() || !Sets.intersection(values, getInclude()).isEmpty();

    }
}
